/**
 * Title:        General Utilities<p>
 * Description:  Useful general utility classes<p>
 *
 */
package com.timber.util;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.metal.*;
import javax.swing.plaf.basic.*;


class SLComboBoxUI extends MetalComboBoxUI
{
  protected ComboPopup createPopup()
  {
    BasicComboPopup popup = new BasicComboPopup( comboBox )
      {
	protected JScrollPane createScroller()
	{
	  return new JScrollPane( list,
				  ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
				  ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
	}

	public void show()
	{
	  Dimension popupSize = ( ( SLComboBox )comboBox ).getPopupSize();

	  popupSize.setSize( popupSize.width,
			     getPopupHeightForRowCount( comboBox.getMaximumRowCount() ) );

	  Rectangle popupBounds = computePopupBounds( 0,
						      comboBox.getBounds().height,
						      popupSize.width,
						      popupSize.height );

	  scroller.setHorizontalScrollBarPolicy( ( ( SLComboBox )comboBox ).getHorizontalScrollBarPolicy() );
	  scroller.getHorizontalScrollBar().setRequestFocusEnabled( false );
	  scroller.setMaximumSize( popupBounds.getSize() );
	  scroller.setPreferredSize( popupBounds.getSize() );
	  scroller.setMinimumSize( popupBounds.getSize() );

	  list.invalidate();            

	  int selectedIndex = comboBox.getSelectedIndex();
	  if( selectedIndex == -1 )
	  {
            list.clearSelection();
	  }
	  else
	  {
            list.setSelectedIndex( selectedIndex );
	  }

	  list.ensureIndexIsVisible( list.getSelectedIndex() );

	  setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );

	  show( comboBox, popupBounds.x, popupBounds.y );
	}
      };

    popup.getAccessibleContext().setAccessibleParent( comboBox );

    return popup;
  }
}

public class SLComboBox extends JComboBox
{
  protected int		popupWidth = 0;
  protected int		horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;


  public SLComboBox()
  {
    super();
    setUI( new SLComboBoxUI() );
  }

  public SLComboBox( ComboBoxModel aModel )
  {
    super( aModel );
    setUI( new SLComboBoxUI() );
  }

  public SLComboBox( final Object[] items )
  {
    super( items );
    setUI( new SLComboBoxUI() );
  }

  public SLComboBox( Vector items )
  {
    super( items );
    setUI( new SLComboBoxUI() );
  }

  public void setPopupWidth( int width )
  {
    popupWidth = width;
  }

  public Dimension getPopupSize()
  {
    Dimension size = getSize();

    if( popupWidth < 1 )
      popupWidth = size.width;

    return new Dimension( popupWidth, size.height );
  }

  public int getHorizontalScrollBarPolicy()
  {
    return horizontalScrollBarPolicy;
  }

  public void setHorizontalScrollBarPolicy( int val )
  {
    switch( val )
    {
      case ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS:
      case ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED:
      case ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER:
	horizontalScrollBarPolicy = val;
	break;

      default:
	horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER;
	break;
    }
  }
}
